let app = getApp()
Page({
  data: {
    url: '',
    userInfo: {},
    isAgreed: false,
    isChecking: false 
  },
  
  onAgreeChange(e) {
    this.setData({
      isAgreed: e.detail.value
    })
  },
  
  checkPhoneExists(phone) {
    return new Promise((resolve, reject) => {
      wx.request({
        url: this.data.url + 'user/checkPhone',
        method: "GET",
        data: { phone }, 
        success: (resp) => {
          if (resp.data.code === 200) {
            resolve(resp.data.data);
          } else {
            reject(new Error('验证手机号失败'));
          }
        },
        fail: () => {
          reject(new Error('网络错误，无法验证手机号'));
        }
      })
    });
  },
  
  async toRegister(e) {
    if (this.data.isChecking) return;
    this.setData({ isChecking: true });
    
    const formData = e.detail.value;
    
    if (!formData.account.trim()) {
      wx.showToast({
        title: '请输入注册信息',
        icon: 'none',
        duration: 2000
      })
      this.setData({ isChecking: false });
      return;
    }
    
    if (!formData.password.trim()) {
      wx.showToast({
        title: '请输入密码',
        icon: 'none',
        duration: 2000
      })
      this.setData({ isChecking: false });
      return;
    }
    
    if (!formData.confirmPassword.trim()) {
      wx.showToast({
        title: '请确认密码',
        icon: 'none',
        duration: 2000
      })
      this.setData({ isChecking: false });
      return;
    }
    
    if (!formData.nickname.trim()) {
      wx.showToast({
        title: '请输入昵称',
        icon: 'none',
        duration: 2000
      })
      this.setData({ isChecking: false });
      return;
    }
    
    if (!formData.phone.trim()) {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none',
        duration: 2000
      })
      this.setData({ isChecking: false });
      return;
    }
    
    const phoneReg = /^1[3-9]\d{9}$/;
    if (!phoneReg.test(formData.phone)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none',
        duration: 2000
      })
      this.setData({ isChecking: false });
      return;
    }
    
    try {
      const phoneExists = await this.checkPhoneExists(formData.phone);
      if (phoneExists) {
        wx.showToast({
          title: '该手机号已注册',
          icon: 'none',
          duration: 2000
        })
        this.setData({ isChecking: false });
        return;
      }
      
      if (formData.password !== formData.confirmPassword) {
        wx.showToast({
          title: '两次密码不一致请检查',
          icon: 'error',
          duration: 2000
        })
        this.setData({ isChecking: false });
        return;
      }
      
      if (!this.data.isAgreed) {
        wx.showToast({
          title: '请同意注册协议',
          icon: 'none',
          duration: 2000
        })
        this.setData({ isChecking: false });
        return;
      }
      
      wx.request({
        url: this.data.url + 'user',
        method: "POST",
        data: formData,
        success: (resp) => {
          if (resp.data.code === 200) {
            wx.redirectTo({
              url: '/pages/my/register/registerSuccess/registerSuccess'
            })
          } else {
            wx.showToast({
              title: resp.data.msg || '注册失败',
              icon: 'none',
              duration: 2000
            })
          }
        },
        fail: () => {
          wx.showToast({
            title: '网络错误，注册失败',
            icon: 'none',
            duration: 2000
          })
        },
        complete: () => {
          this.setData({ isChecking: false });
        }
      })
      console.log(formData);
    } catch (error) {
      wx.showToast({
        title: error.message,
        icon: 'none',
        duration: 2000
      })
      this.setData({ isChecking: false });
    }
  },
  
  onLoad(options) {
    this.setData({
      url: app.globalData.url
    })
  },

  onReady() {},
  onShow() {},
  onHide() {},
  onUnload() {},
  onPullDownRefresh() {},
  onReachBottom() {},
  onShareAppMessage() {}
})
